#include "buffer.h"
#include <stdio.h>
//extern void buffer_init();
//extern unsigned long BUFFER_END;
//extern struct buffer_head * start_buffer;
//extern int NR_BUFFERS;
//extern char end[];

struct task_struct init_task;
struct task_struct *current = &init_task, *last_task_used_math = NULL;


void usage(void)
{
        printk("Usage: M1 0/1 diskimg\r\n");
	panic("0: read; 1 install");
}

void sys_cpsh(char *filename)
{
	int id, c, did;
	char buffer[BLOCK_SIZE];

	if ((id=open(filename,O_RDWR,0))<0)
                panic("sys_cpsh: Unable to open src file");

	sys_setup();

	sys_mkdir("dev", 0);
	sys_mknod("dev/tty0",I_CHAR_SPECIAL,0x400);

	sys_mkdir("bin", 0);
        if((did = sys_creat("bin/sh", S_IFBLK|0777))<0)
		panic("sys_cpsh: Unable to open dest file");
	
        while(c = read(id, buffer, BLOCK_SIZE)){
                if(c==EOF)
                        return;
                else
                        sys_write(did, buffer, c);
        }

        sys_sync();
}

int main(int argc, char **argv)
{
        if (argc != 3 && argc != 4)
                usage();

	buffer_init();
	hd_init(argv);

	switch(argv[1][0]){
		case '0':
			printk("Read system\r\n");
			sys_setup();
			break;
		case '1':
			printk("Write supper\r\n");
			sys_install();
			break;
		case '2':
                        printk("List dir\r\n");
                        sys_ls(argv[3]);
                        break;
                case '3':
                        printk("cp sh\r\n");
                        sys_cpsh(argv[3]);
                        break;
                case '4':
                        printk("Add /dev/tty0 in root\r\n");
			sys_setup();
                        sys_mknod("dev/tty0",I_CHAR_SPECIAL,0x400);
			break;
                case '5':
                        printk("Write files in dir\r\n");
                        sys_writefile(argv[3]);
                        break;
                case '6':
                        printk("Read files in dir\r\n");
                        sys_readfile(argv[3]);
                        break;
			
		default:
			printk("flag error 0x%x\r\n", argv[1][0]);
			usage();
	}
	return 0;
}


